package com.zhida.chatservice.service.impl;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zhida.chatservice.service.OfflineService;
import com.zhida.common.entity.Result;
import com.zhida.common.utils.ObjectUtil;
import jakarta.annotation.Resource;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

/**
 * Create by zhh 2024/7/16 7:48
 */
@Service
public class OfflineServiceImpl implements OfflineService {

    @Resource
    final RedisTemplate<String,String> redisTemplate;

    public OfflineServiceImpl(RedisTemplate<String, String> redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

    @Override
    public Result<?> getOfflineMsg(String username) throws JsonProcessingException {
//        从缓存中取离线消息
        String offlineMsg = redisTemplate.opsForValue().get("WS:OFFLINE:" + username);
        System.out.println(offlineMsg);
        return Result.success(new ObjectMapper().readValue(offlineMsg, ArrayList.class));
    }
}
